'use strict';

// 1.服务是否存在
// 2.方法是否存在
// 3.成功响应
// 4.响应失败

describe('fbdApp fbdService module', function() {

  var $rootScope;

  var fbdService;

  var $httpBackend;

  beforeEach(module('fbdApp.auxModule'));

  beforeEach(inject(function(_$rootScope_, _fbdService_, _$httpBackend_) {
    $rootScope = _$rootScope_;
    $httpBackend = _$httpBackend_;
    fbdService = _fbdService_;
  }));

  afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

  it('should be defined', function(done) {
    expect(fbdService).not.toBe(null);
    expect(fbdService.test).toBeDefined();
    expect(fbdService.save).toBeDefined();
    return done();
  });

  describe('test service', function() {

    var fakedUrl = 'http://postRules';

    it('mock success response with text', function(done) {

      var postData = {
        rule: {
          mode: 'signs',
          beginText: '<<<<',
          type: "1"
        },
        text: {
          content: '<<<<section1<<<<section2<<<<section3',
          fileName: 'aaa.fbd'
        }
      };

      var expectedData = {
        rule: {
          mode: 'signs',
          beginText: '<<<<',
          type: 1,
          expression: '1232C'
        },
        result: [{
          text: '<<<<11',
          location: [10, 6]
        }, {
          text: '<<<<22',
          location: [20, 6]
        }]
      };

      var promise = fbdService.test(fakedUrl, postData);

      $httpBackend.expect('POST', fakedUrl, postData).respond(
        expectedData);

      var res, err;
      // var deferred = $q.defer();
      promise.then(function(data) {
        res = data;
      }, function(data) {
        err = data;
      });

      $httpBackend.flush();
      $rootScope.$apply();


      expect(res).toEqual(expectedData);
      expect(err).toBeUndefined();

      return done();

    });

    it('mock success response with file', function(done) {

      var postData = {
        rule: {
          mode: 'signs',
          beginText: '<<<<',
          type: "1"
        },
        text: {
          content: undefined,
          fileName: 'aaa.fbd'
        }
      };


      var expectedData = {
        rule: {
          mode: 'signs',
          beginText: '<<<<',
          type: 1,
          expression: '1232C'
        },
        result: [{
          text: '<<<<11',
          location: [10, 6]
        }, {
          text: '<<<<22',
          location: [20, 6]
        }]
      };

      var promise = fbdService.test(fakedUrl, postData);

      $httpBackend.expect('POST', fakedUrl, postData).respond(
        expectedData);

      var res, err;
      // var deferred = $q.defer();
      promise.then(function(data) {
        res = data;
      }, function(data) {
        err = data;
      });

      $httpBackend.flush();
      $rootScope.$apply();


      expect(res).toEqual(expectedData);
      expect(err).toBeUndefined();

      return done();

    });

    it('mock error response', function(done) {

      var wrongData = {
        rule: {
          // mode: 'signs',
          beginText: '<<<<',
          type: "2"
        },
        text: {
          content: undefined,
          fileName: 'aaa.fbd'
        }
      };


      var promise = fbdService.test(fakedUrl, wrongData);
      $httpBackend.expect('POST', fakedUrl, wrongData).respond(400,
        '参数错误');
      $httpBackend.flush();
      var res, err;
      promise.then(function(data) {
        res = data;
        // console.log(res);
      }, function(data) {
        err = data;
      });

      $rootScope.$apply();

      expect(res).toBeUndefined();
      expect(err).toBe('参数错误');
      return done();
    });
  });

  describe('save service', function() {

    var fakedUrl = 'http://addRules';
    var addData = {
      mode: 'signs',
      name: 'title',
      type: 1,
      beginText: '<<<<',
      expression: "125433,*,0124587"
    };

    var wrongData = {
      name: 'title',
      type: '1',
      beginText: '<<<<'
    };

    var expectedData = {
      __v: 0,
      key: 'title',
      name: 'title',
      mode: 'signs',
      type: 1,
      beginText: '<<<<',
      id: '5799b175643e9ddc0aca5211',
      expression: '125433,*,0124587'
    };

    // var expectedData = ['section1', 'section2', 'section3'];
    it('success response', function(done) {
      var promise = fbdService.save(fakedUrl, addData);
      $httpBackend.expect('POST', fakedUrl, addData).respond(
        expectedData);

      $httpBackend.flush();

      var res, err;
      // var deferred = $q.defer();
      promise.then(function(data) {
        res = data;
      }, function(data) {
        err = data;
      });

      $rootScope.$apply();

      expect(res).toEqual(expectedData);
      expect(err).toBeUndefined();

      return done();
    });

    it('error response', function(done) {
      var promise = fbdService.save(fakedUrl, wrongData);
      $httpBackend.expect('POST', fakedUrl, wrongData).respond(400,
        '参数错误');
      $httpBackend.flush();
      var res, err;
      promise.then(function(data) {
        res = data;
        // console.log(res);
      }, function(data) {
        err = data;
      });

      $rootScope.$apply();
      expect(res).toBeUndefined();
      expect(err).toBe('参数错误');
      return done();
    });
  });

});
